package com.project.controller;

import com.project.bean.ReplyBean;
import com.project.bean.ThemeBean;
import com.project.bean.UserBean;
import com.project.service.IReplyService;
import com.project.service.IThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("reply")
public class ReplyController {
    @Autowired
    private IReplyService replyService;
    @Autowired
    private IThemeService themeService;

    @RequestMapping("findById")
    public ThemeBean findById(HttpServletRequest req) {
        Integer id = (Integer) req.getSession().getAttribute("themeId");
        return themeService.findById(id);
    }

    @RequestMapping("findAll")
    public List<ReplyBean> findAll(HttpServletRequest req) {
        Integer id = (Integer) req.getSession().getAttribute("themeId");
        return replyService.findAll(id);
    }

    @RequestMapping("add")
    public String add(ReplyBean reply, HttpServletRequest req) {
        Integer themeId = (Integer) req.getSession().getAttribute("themeId");
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        reply.setUserId(user.getId());
        reply.setThemeId(themeId);
        replyService.add(reply);
        return "ok";
    }
}
